package com.qingmi.farmer.ui.buy;

import android.content.Context;
import android.content.Intent;
import android.databinding.Observable;
import android.view.View;

import com.qingmi.farmer.BR;
import com.qingmi.farmer.R;
import com.qingmi.farmer.data.ActionsCode;
import com.qingmi.farmer.data.DataSetting;
import com.qingmi.farmer.data.model.GoodsInfo;
import com.qingmi.farmer.data.model.GoodsType;
import com.qingmi.farmer.data.model.Supplier;
import com.qingmi.farmer.data.repository.data.GoodsDataRepository;
import com.qingmi.farmer.data.request.GetGoodsListReq;
import com.qingmi.farmer.data.request.GetGoodsTypeReq;
import com.qingmi.farmer.data.request.GetSupplierReq;
import com.qingmi.farmer.databinding.ActivityGoodsListBinding;
import com.wjjun.base.ui.base.ListActivity;
import com.qingmi.farmer.viewmodel.GoodsItemViewModel;
import com.wjjun.basemvpvm.data.exception.MyException;
import com.wjjun.basemvpvm.data.model.PageEntity;
import com.wjjun.basemvpvm.data.observer.DefaultObserver;
import com.wjjun.basemvpvm.viewmodel.BaseViewModel;
import com.wjjun.customview.views.dropdownmenu.OnItemMenuClickListener;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import me.tatarka.bindingcollectionadapter2.ItemBinding;


/**
 * 商品列表
 */
public class GoodsListActivity extends ListActivity<GoodsListViewModel, ActivityGoodsListBinding> {

    public static void start2Goods(Context context) {
        Intent intent = new Intent(context, GoodsListActivity.class);
        intent.putExtra(ActionsCode.ACTION, ActionsCode.ACTION_GOODS_LIST);
        context.startActivity(intent);
    }

    @Inject
    GoodsDataRepository goodsDataRepository;

    private GetGoodsListReq req = new GetGoodsListReq();

    private int action;

    @Override
    public int getLayoutId() {
        return R.layout.activity_goods_list;
    }

    @Override
    protected GoodsListViewModel initViewModel() {
        return new GoodsListViewModel();
    }

    @Override
    protected void initContent() {
        super.initContent();
        goodsDataRepository.bind(this);

        action = getIntent().getIntExtra(ActionsCode.ACTION, ActionsCode.ACTION_GOODS_LIST);

        if (action == ActionsCode.ACTION_GOODS_LIST) {
            req.setClassId(DataSetting.GOODS_TYPE_GOODS);
        } else if (action == ActionsCode.ACTION_TECH_LIST) {
            req.setClassId(DataSetting.GOODS_TYPE_SERVICE);
        }

        viewModel.menuAdapter.set(new GoodsMenuAdapter(this));

        //添加监听
        viewModel.search.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                req.setSearch(viewModel.search.get());
                refreshData();
            }
        });
    }

    @Override
    protected void initData() {
        req.reset();
        goodsDataRepository.getGoodsType(new DefaultObserver<List<GoodsType>>() {

            @SuppressWarnings("ConstantConditions")
            @Override
            public void onNext(List<GoodsType> goodsTypes) {
                if (goodsTypes != null && !goodsTypes.isEmpty()) {
                    for (GoodsType item : goodsTypes) {
                        if (item.getChildParameter() == null) {
                            item.setChildParameter(new ArrayList<GoodsType>());
                        }
                        item.getChildParameter().add(0, new GoodsType(item.getId(), getString(R.string.format_all,item.getName())));
                    }
                }
                viewModel.menuAdapter.get().setTypeList(goodsTypes);
            }

            @Override
            public void onError(MyException e) {
                showToast(e.getMessage());
            }
        }, new GetGoodsTypeReq(DataSetting.GOODS_TYPE_GOODS));

        refreshData();
    }

    @Override
    public void onListItemBind(ItemBinding itemBinding, int position, Object item) {
        if (item instanceof GoodsItemViewModel) {
            itemBinding.set(BR.viewModel, R.layout.layout_item_goods).bindExtra(BR.presenter, viewGroupPresenter);
        }
    }

    @Override
    protected void onListItemClick(View view, BaseViewModel item) {
        super.onListItemClick(view, item);
        switch (view.getId()) {
            case R.id.vg_item:
                if (item instanceof GoodsItemViewModel) {
                    GoodsDetailActivity.start(GoodsListActivity.this, ((GoodsItemViewModel) item).id);
                }
                break;
            case R.id.iv_add_trolley:   //添加购物车
                if (item instanceof GoodsItemViewModel) {
                }
                break;
        }
    }


    public final OnItemMenuClickListener itemMenuClickListener = new OnItemMenuClickListener() {
        @SuppressWarnings("ConstantConditions")
        @Override
        public void onMenuItemClick(int index) {
            if (index == 0) {    //全部
                viewModel.menuAdapter.get().setDefaultValue();
                req.reset();
                autoRefresh();
            }
        }

        @Override
        public void onContentItemClick(int index, Object item1, Object item2) {
            if (index == 1) {   //类型
                if (item2 != null) {
                    req.setTypeId((String) item2);
                } else {
                    req.setTypeId((String) item1);
                }
            } else if (index == 2) {    //商家
                req.setSupplierId((String) item1);
            } else if (index == 3) {    //排序
                req.setOrderType((Integer) item1);
            }
            autoRefresh();
        }
    };

    @Override
    public void refreshData(boolean init) {
        super.refreshData(init);
        req.setPageIndex(1);
        goodsDataRepository.getGoodsList(new DefaultObserver<PageEntity<List<GoodsInfo>>>() {
            @Override
            public void onNext(PageEntity<List<GoodsInfo>> listPageEntity) {
                List<GoodsItemViewModel> list = new ArrayList<>();
                if (listPageEntity != null && listPageEntity.getEntity() != null) {  //空数据
                    for (GoodsInfo item : listPageEntity.getEntity()) {
                        GoodsItemViewModel itemViewModel = new GoodsItemViewModel(item);
                        list.add(itemViewModel);
                    }
                }
                resetData(list);
            }

            @Override
            public void onError(MyException e) {
                setError(e.getMessage());
            }
        }, req);


        goodsDataRepository.getSupplier(new DefaultObserver<List<Supplier>>() {
            @SuppressWarnings("ConstantConditions")
            @Override
            public void onNext(List<Supplier> suppliers) {
                viewModel.menuAdapter.get().setSupplierList(suppliers);
            }

            @Override
            public void onError(MyException e) {
                showToast(e.getMessage());
            }
        }, new GetSupplierReq(DataSetting.GOODS_TYPE_GOODS));
    }

    @Override
    public void loadData() {
        super.loadData();
        req.setPageIndex(req.getPageIndex() + 1);
        goodsDataRepository.getGoodsList(new DefaultObserver<PageEntity<List<GoodsInfo>>>() {
            @Override
            public void onNext(PageEntity<List<GoodsInfo>> listPageEntity) {
                if (listPageEntity == null || listPageEntity.getEntity() == null) {  //空数据
                    finishLoadMore(true);
                } else {
                    List<GoodsItemViewModel> list = new ArrayList<>();
                    for (GoodsInfo item : listPageEntity.getEntity()) {
                        GoodsItemViewModel itemViewModel = new GoodsItemViewModel(item);
                        list.add(itemViewModel);
                    }
                    addData(list, listPageEntity.getPageSize());
                }
            }

            @Override
            public void onError(MyException e) {
                finishLoadMoreError();
                showToast(e.getMessage());
            }
        }, req);

    }


}
